<?php

namespace app\backstage\validate;

use think\Validate;

class WebsiteValidate extends Validate
{
    protected $rule = [
        'id' => 'require|number|gt:0',
        'agent_id' => 'require',
        'nIds' => 'require|checkIds',
        'novel_id' => 'require|number|gt:0',
        'index' => 'require|number|gt:0',
        'point' => 'require|number|egt:0',
        'title'     => 'require',
        'contentSource' => 'require|in:0,1',
        'sort' => 'require|number|egt:0',
        'banner' => 'require',
        'status' => 'in:1,2',
        'type' => 'require|in:1,2,0',
        'is_random' => 'require|in:0,1',
        'freq' => 'require|in:1,2,0',
        'ids' => 'require|array',
        'seat' => 'require|in:1,2,3,0',
        'content_type' => 'require|in:1,2',
        'activity_id' => 'number|gt:0',
        'table_novel_id' => 'array',
        'display' => 'require|in:0,1',
        'key' => 'require|in:add_desktop,bind_number,calendar,read,pay_give',

        'eTitle' => 'require',
        'description' => 'require',
        'startDate' => 'require|checkStartAndEnd',
        'endDate' => 'require|checkStartAndEnd',
        'remindMinutes' => 'require|array',
        'rrule' => 'require',
        'chapter_num' => 'require|number|gt:0',
        'add_type' => 'require|in:1,2',
        'quick_apps' => 'require|array',
        'recommend_titles' => 'require|array',
        'novel_type' => 'require|in:1,2',
        'freq_ids' => 'require|array',
        'novel_ids' => 'require|array',
    ];

    protected $message = [
        'nIds.require' => '小说id不能为空',
        'agent_id.require' => '请选择代理商',
        'point.require' => '请填写书点',
        'point.number' => '书点必须是正确的数字',
        'point.egt' => '书点必须>=0',
        'remindMinutes' => '提醒时间必须选择',
        'chapter_num' => '章节数必须是正整数',
        'freq.require' => '请选择频道',
        'type.require' => '请选择类型',
        'add_type.require' => '请选择添加类型',
        'quick_apps.require' => '请选择代理商',
        'recommend_titles.require' => '请选择推荐位',
        'novel_type.require' => '请选择书籍人群类型',
        'freq_ids.require' => '请选择推荐位所属的上级频道',
    ];

    protected $scene = [
        'addBookshelfNovelIds' => ['agent_id', 'nIds', 'type'],
        'delBookshelfNovel' => ['agent_id', 'novel_ids', 'type'],
        'setSignPoint' => ['quick_id', 'index', 'point'],
        'delRecommend' => ['ids'],
        'editTaskForm' => ['quick_id', 'key'],
        'switchRandom' => ['id', 'is_random'],
        'addRecommendNovel' => ['id', 'nIds','type'],
        'setRecommendNovelSort' => ['id', 'sort'],
        'delRecommendNovel' => ['id'],
        'delBanner' => ['ids'],
        'setTaskDisplay' => ['key', 'display', 'quick_id'],
        'calendar' => ['eTitle', 'description', 'startDate', 'endDate', 'remindMinutes', 'rrule'],
        'taskRead' => ['chapter_num'],
        'plAddRecommendNovels' => ['add_type', 'quick_apps', 'novel_type', 'recommend_titles','freq_ids','nIds'],
    ];

    // 自定义验证规则
    protected function checkIds($value, $rule, $data = [])
    {
        if (!preg_match('#^[1-9][0-9]*(,[1-9][0-9]*)*$#', $value)) {
            return '参数异常，请检查是否为正确的id，并且格式是否正确';
        }
        return true;
    }


    public function sceneAddRecommend()
    {
        return $this->only(['agent_id', 'title', 'freq']);
    }


    public function sceneEditRecommend()
    {
        return $this->only(['id', 'title']);
    }


    public function sceneSetBanner()
    {
        return $this->only(['agent_id', 'type', 'seat', 'banner', 'activity_id'])
            ->remove('type', 'in')
            ->append('type', 'in:0,1,2');
    }

    public function sceneEditTask()
    {
        return $this->only(['quick_id', 'key', 'title', 'desc', 'point', 'sort'])
            ->remove('sort', 'egt')
            ->append('sort', 'gt:0');
    }

    // 验证时间为分
    protected function checkStartAndEnd($value,$rule,$data=[])
    {
        $pattern = "/^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/";
        if (!preg_match($pattern, $value)) {
            return '时间格式异常';
        }
        return true;
    }
}